<?php
class Publishers extends CI_Controller{
	function __construct(){
		parent::__construct();
		//判断session
		if (!$this->session->userdata('name')){
			header("Location: /");
		}
	}
	
	function index(){
		$this->load->model('publishers_model');
		$offset = $this->input->get('offset');
		
		$this->load->library('pagination');
		$page_config = render_pagination_config_array();	
		$page_config['base_url'] = '/publishers/?';
		$page_config['total_rows'] = $this->publishers_model->get_all_rows();
		$this->pagination->initialize($page_config);
		$pager_link	= $this->pagination->create_links();
		
		$publishers_list = $this->publishers_model->get_consumable_by_page($page_config['per_page'],$offset);
		
		$data['sidebar'] = array('editorial','publishers');
		$data['query'] = $publishers_list;
		$data['page'] = $pager_link;


		load_view('publishers',$data);
	}
	
	function new_publishers(){
		$data['sidebar'] = array('editorial','publishers/new_publishers');
		$data['query'] = '';
		load_view('publishers_file',$data);
	}
	
	function modify_publishers($id){	
		$this->load->model('publishers_model');
		$obj = new Publishers_model($id);
		$data['sidebar'] = array('editorial','publishers/modify_publishers');
		$data['query'] = array(
			'id' => $id,
			'name' => $obj->publishername,
		);
		load_view('publishers_file',$data);
	}
	
	function execute(){
		$id = $this->input->post('id');
		$name = trim($this->input->post('name'));
	
		$this->load->model('publishers_model');
		$obj = new Publishers_model($id);
		$obj->publishername = $name;
		
		if($id){
			$obj->update();
		}else{
			$obj->save();	
		}
		header("Location: /publishers");
	}
	
	function del_publishers($id){
		$this->load->model('publishers_model');
		$obj = new Publishers_model($id);
		$obj->delete();
		header("Location: /publishers");
	}
}